package com.union.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Button;


import com.union.unionht.R;

import java.util.concurrent.ConcurrentHashMap;


public class DialogUtils
{
	private static ConcurrentHashMap<String, Dialog> loadingCache = new ConcurrentHashMap<String, Dialog>();
	private static ConcurrentHashMap<Object, Dialog> errorCache = new ConcurrentHashMap<Object, Dialog>();

	private DialogUtils()
	{
	}

	public static void showLoading(Object context, String refreshMethod)
	{
		Dialog loadingDialog;
		if (!loadingCache.isEmpty())
		{
			return;
		}
		if (isFragment(context))
		{
			loadingDialog = createDialog(((Fragment) context).getActivity(), R.style.Dialog_Loading);
		} else
		{
			loadingDialog = createDialog(((FragmentActivity) context), R.style.Dialog_Loading);
		}
		loadingCache.put(refreshMethod, loadingDialog);
		loadingDialog.setContentView(R.layout.dialog_loading);
		loadingDialog.setCanceledOnTouchOutside(false);
		if (!loadingDialog.isShowing())
		{
			loadingDialog.show();
		}
	}



	public static void showExit(Context context)
	{
		Dialog exitDialog = createDialog(context, R.style.Dialog_Common);
		View view = View.inflate(exitDialog.getContext(), R.layout.dialog_exit, null);
		exitDialog.setContentView(view);
		exitDialog.setCanceledOnTouchOutside(false);
		exitDialog.setCancelable(false);
		initExitBtnListener(context, exitDialog, view);
		exitDialog.show();
	}

	/***
	 *  拨打电话
	 * @param context
	 * @param tel
	 */
	public static void showTelDialog(final Activity context, final String tel)
	{
		new AlertDialog.Builder(context).setMessage("呼叫:" + tel).setPositiveButton("确定", new DialogInterface.OnClickListener()
		{
			public void onClick(DialogInterface dialog, int which)
			{
				IntentUtils.startDialNumberIntent(context, tel);
			}
		}).setNegativeButton("取消", new DialogInterface.OnClickListener()
		{
			@Override
			public void onClick(DialogInterface dialog, int which)
			{
				dialog.dismiss();
			}
		}).show();
	}

	public static void dismissLoading(String refreshMethod)
	{
		try {
			Dialog dialog = loadingCache.get(refreshMethod);
			if (dialog != null)
            {
				if (dialog.isShowing())
                dialog.dismiss();
				if (loadingCache.containsValue(refreshMethod))
                loadingCache.remove(refreshMethod);
            }
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void dismissError(Object context)
	{
		if (isFragment(context))
		{
			errorCache.get(((Fragment) context).getActivity()).dismiss();
			errorCache.remove(((Fragment) context).getActivity());
		} else
		{
			errorCache.get(context).dismiss();
			errorCache.remove(context);
		}
	}

	public static void dismissExit(Dialog exitDialog)
	{
		exitDialog.dismiss();
	}

	public static Dialog createDialog(Context context, int styleRes)
	{
		return new Dialog(context, styleRes);
	}

	private static boolean isFragment(Object context)
	{
		return context instanceof Fragment;
	}



	private static void initExitBtnListener(final Context context, final Dialog exitDialog, View view)
	{
		Button exit = (Button) view.findViewById(R.id.fancybtn_home_personal_exit_confirm);
		exit.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				SessionUtils.clearUserId();
				dismissExit(exitDialog);
				((Activity) context).finish();
			}
		});
		Button cancel = (Button) view.findViewById(R.id.fancybtn_home_personal_exit_cancel);
		cancel.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				dismissExit(exitDialog);
			}
		});
	}

}
